package cn.com.czpb.client.module.subscribe.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import cn.com.czpb.client.R;
import cn.com.czpb.client.api.InterfaceJsonfile;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.model.base.BaseEntity;
import cn.com.czpb.client.model.subscribe.FocusUserDetailBean;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondBean;
import cn.com.czpb.client.model.subscribe.SubscribeListSecondEntity;
import cn.com.czpb.client.module.subscribe.RefreshSubScribeListListener;
import cn.com.czpb.client.module.subscribe.adapter.SubscribeMoreListAdapter;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.DeviceUtils;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.Unbinder;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/8/15.
 */

public class SubscribeDetailFragment extends BaseFragment implements RefreshSubScribeListListener, BaseQuickAdapter.OnItemChildClickListener {
    @BindView(R.id.tv_describe_id)
    TextView tvDescribeId;
    @BindView(R.id.rv_list_id)
    RecyclerView rvListId;

    // TODO: 2017/8/15  id改成动态 
    private String currentid = "1";
    Unbinder unbinder;
    private SubscribeMoreListAdapter adapter;
    private String did;
    public static SubscribeDetailFragment getIntance(String did){
        SubscribeDetailFragment fragment=new SubscribeDetailFragment();
        Bundle bundle=new Bundle();
        bundle.putString("did",did);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    public int setMyContentView() {
        return R.layout.fragment_subscribe_detail;
    }

    @Override
    public void initView() {
        did=getArguments().getString("did");
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvListId.setLayoutManager(manager);
        adapter = new SubscribeMoreListAdapter(null);
    adapter.setOnItemChildClickListener(this);
        rvListId.setAdapter(adapter);
    }

    @Override
    public void initData() {

    }

    @Override
    public void refresh(FocusUserDetailBean bean) {
        tvDescribeId.setText(bean.getDesc());
        // TODO: 2017/8/15 动态id 
        getWriterList(currentid);
    }
private int page =1;
    private int pageSize =15;
    //获取类似达人号
        private void getWriterList(String id) {
            Map<String, String> map = new HashMap();
            map.put("siteid", InterfaceJsonfile.SITEID);
            map.put("id", id);
            map.put("did", did);
            LogUtils.e("did----"+did);
            if (spu.getUser()!=null){
                map.put("uid",spu.getUser().getUid());
            }
            map.put("device", DeviceUtils.getMyUUID(activity));
            map.put("Page", page + "");
            map.put("PageSize", pageSize + "");
            Factory.provideHttpService().subscribeSecondList(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Action1<SubscribeListSecondEntity>() {
                        @Override
                        public void call(SubscribeListSecondEntity entity) {
                            setListData2(entity.data);
                        }
                    }, new Action1<Throwable>() {
                        @Override
                        public void call(Throwable throwable) {
                            adapter.loadMoreFail();
                        }
                    });
        }
    public void setListData2(List< SubscribeListSecondBean > list) {
        adapter.setNewData(list);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        SubscribeListSecondBean bean = (SubscribeListSecondBean) adapter.getItem(position);
        switch (view.getId()){
            case R.id.img_subscribe_id:
                //订阅
                LogUtils.i("订阅");
                focusUser(bean.getUid());
                break;
            case R.id.img_remove_subscribe_id:
                //订阅
                LogUtils.i("取消订阅");
                removeFocusUser(bean.getUid());
                break;
            case R.id.ll_root_id:
                //作者个人主页
                PageCtrl.start2SubscribeDetailActivity(activity,bean.getUid(),bean.getTid());
                break;
        }
    }

    //订阅达人号
    private void focusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().focusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        getWriterList(currentid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
    //取消订阅
    private void removeFocusUser(String uid){
        Map<String ,String> map = new HashMap<>();
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }
        map.put("device",DeviceUtils.getMyUUID(activity));
        map.put("focusid",uid);
        Factory.provideHttpService().removeFocusUser(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity>() {
                    @Override
                    public void call(BaseEntity baseEntity) {
                        getWriterList(currentid);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });

    }
}
